package com.zhiyun.service.impl;

import com.zhiyun.domain.CourseContent;
import com.zhiyun.mapper.CourseContentMapper;
import com.zhiyun.service.CourseContentService;
import com.zhiyun.util.Db;
import com.zhiyun.vo.AppData;
import com.zhiyun.vo.CourseFile;
import com.zhiyun.vo.Result;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

/**
 * @author zc
 * @Date 2025/4/29 上午11:41
 */
public class CourseContentServiceImpl implements CourseContentService {
    CourseContentMapper courseContentMapper = Db.getMapper(CourseContentMapper.class);
    @Override
    public Result findByCourseUnitId(int unitId) {
        List<CourseContent> list = courseContentMapper.findByCourseUnitId(unitId);
        return Result.ok(list);
    }

    @Override
    public Result getCourseContentFile(Integer courseContentId) {
        CourseContent courseContent = courseContentMapper.selectByPrimaryKey(courseContentId);
        if (courseContent == null) {
            return Result.fail("该内容不存在");
        }
        String resourceSrc = courseContent.getResourceSrc();
        File file = new File(AppData.serverContentSrcPrefix + resourceSrc);
        if (file.exists()) {
            byte[] fileContent = new byte[(int) file.length()];
            FileInputStream fileInputStream = null;
            try {
                fileInputStream = new FileInputStream(file);
                fileInputStream.read(fileContent);
            } catch (IOException e) {
                e.printStackTrace();
                Result.fail("下载文件失败");
            } finally {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return Result.ok("下载文件成功",new CourseFile(courseContent.getResourceSrc(), fileContent));
        }else {
            return Result.fail("文件已丢失");
        }
    }

    @Override
    public Result uploadCourseContent(CourseContent courseContent, CourseFile courseFile) {
        FileOutputStream fileOutputStream = null;
        try {
            Db.startTransaction();
            CourseContent oldCourseContent = courseContentMapper.selectBySrc(courseFile.getFileName());
            if (oldCourseContent != null) {
                return Result.fail("文件已存在");
            }
            int rows = courseContentMapper.insert(courseContent);
            if (rows == 0) {
                return Result.fail("上传失败");
            }
            File file = new File(AppData.serverContentSrcPrefix + courseContent.getResourceSrc());
            fileOutputStream = new FileOutputStream(file);
            byte[] fileContent = courseFile.getFileContent();
            fileOutputStream.write(fileContent);
            Db.commitTransaction();
        } catch (Exception e) {
            e.printStackTrace();
            Db.rollbackTransaction();
            return Result.fail("上传失败");
        } finally {
            Db.close();
            try {
                if (fileOutputStream != null) {
                    fileOutputStream.close();
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return Result.okMsg("上传成功");
    }

    @Override
    public Result deleteContent(Integer courseContentId) {
        CourseContent courseContent = courseContentMapper.selectByPrimaryKey(courseContentId);
        if (courseContent == null) {
            return Result.fail("删除失败,课程内容不存在");
        }
        try {
            Db.startTransaction();
            courseContentMapper.deleteByPrimaryKey(courseContentId);
            String src = courseContent.getResourceSrc();
            File file = new File(AppData.serverContentSrcPrefix + src);
            boolean b = file.delete();
            if (!b) {
                throw new Exception("删除失败");
            }
            Db.commitTransaction();
        } catch (Exception e) {
            e.printStackTrace();
            Db.rollbackTransaction();
        } finally {
            Db.close();
        }
        return Result.okMsg("删除成功");
    }

}
